#!/usr/bin/env python
# -*-coding:UTF-8-*-
# Author: chenzuoqing@126.com
# Create: 2018/8/3 14:16
# File  : import_menu.py

"""
导入菜单，初始化执行
"""

import sys
import os
import django

project_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(project_dir)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "devops.settings")

django.setup()

from permissions.models import Menu

menus = [
    {
        "path": "/",
        "label": "首页",
        "icon": "dashboard",
        "show": True,
        "children": []
    },
    {
        "path": "/monitor",
        "label": "监控管理",
        "icon": "monitor",
        "show": True,
        "children": [
            {
                "path": "/zabbix",
                "label": "Zabbix",
                "icon": "z",
                "show": True
            },
            {
                "path": "/chart",
                "label": "常用图形",
                "icon": "chart",
                "show": True
            }
        ]
    },
    {
        "path": "/resources",
        "label": "资源管理",
        "icon": "nested",
        "show": True,
        "children": [
            {
                "path": "/idcs",
                "label": "机房管理",
                "icon": "idc",
                "show": True,
            },
            {
                "path": "/cabinets",
                "label": "机柜管理",
                "icon": "cabinet",
                "show": True,
            },
            {
                "path": "/servers",
                "label": "服务器",
                "icon": "server",
                "show": True,
            },
            {
                "path": "/business",
                "label": "业务线",
                "icon": "business",
                "show": True,
            }
        ]
    },
    {
        "path": "/mysql",
        "label": "SQL上线",
        "icon": "mysql",
        "show": True,
        "children": [
            {
                "path": "/edit",
                "label": "SQL提交",
                "icon": "edit",
                "show": True,
            },
            {
                "path": "/audit",
                "label": "SQL审核",
                "icon": "audit",
                "show": True,
            },
            {
                "path": "/database",
                "label": "数据库配置",
                "icon": "database",
                "show": True,
            }
        ]
    },
    {
        "path": "/workorder",
        "label": "工单管理",
        "icon": "workorder",
        "show": True,
        "children": [
            {
                "path": "/edit",
                "label": "提交工单",
                "icon": "edit",
                "show": True,
            },
            {
                "path": "/handle",
                "label": "处理工单",
                "icon": "audit",
                "show": True,
            },
            {
                "path": "/history",
                "label": "历史工单",
                "icon": "history",
                "show": True,
            }
        ]
    },
    {
        "path": "/roles",
        "label": "角色管理",
        "icon": "peoples",
        "show": True,
        "children": [
            {
                "path": "/users",
                "label": "用户管理",
                "icon": "user2",
                "show": True,
            },
            {
                "path": "/group",
                "label": "用户组管理",
                "icon": "group",
                "show": True,
            }
        ]
    },
]


def _import_menu(path, label, icon, show, parent, *args, **kwargs):
    menu = Menu()
    menu.path = path
    menu.title = label
    menu.icon = icon
    menu.show = show
    menu.parent = parent
    menu.save()
    return menu


def import_menus():
    for menu in menus:
        children = menu.pop("children", [])
        menu["parent"] = None
        menu_obj = _import_menu(**menu)
        for child in children:
            child["parent"] = menu_obj
            _import_menu(**child)


def run():
    if Menu.objects.count() != 0:
        print("已经有菜单了，不需要再重复导入！")
        sys.exit(0)

    import_menus()


if __name__ == "__main__":
    run()
